package hn.cch.spring.core.order;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BeanPostProcessorDemo {

    /**
     * 测试BeanPostProcessor顺序
     */
    public static void main(String[] args) {
        // bean工厂
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        // 注册注解配置处理器
        AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);
        List<BeanPostProcessor> beanPostProcessorList = new ArrayList<>(Arrays.asList(
            new FirstPostProcessorImpl(),
            new SecondBeanPostProcessor(),
            new PriorityOrderedImpl(),
            new OrderedBeanPostProcessor(),
            new BeanPostProcessorImpl()
        ));
        beanPostProcessorList.sort(beanFactory.getDependencyComparator());

        beanPostProcessorList.forEach(processor -> processor.postProcessBeforeInitialization(new Object(), ""));

    }

}
